<?php
/**
 * @file rpd.php
 *
 * This is the frontend controller of rpd.
 *
 * Every client-input will be filtered in this file. It incrementaly searches
 * the right php and stores additional input in $_GET.
 *
 *
 * See also:
 *  @see configuration.php
 *  @see bootstrap.php
 *
 * BSD License
 * \n
 * Copyright (c) 2010 Rpd-Team (http://code.google.com/p/rpd)
 *
 * @namespace  rpd
 * @since      0.0.1
 *
 * @todo A110220 - plugin system
 * @todo B110220 - multi languages (in production - not rpd itself)
 *
 */

/**
 *
 * Loads unknown classes dynamicaly.
 * @param string $class the unknown class
 */
function __autoload($class_name)
{
	$class_file = str_replace("_", "/", $class_name);
	$rpdDirectory = dirname(__FILE__);
	if (file_exists($rpdDirectory . "/rpd/" . $class_file . ".php"))
	{
		require_once "rpd/" . $class_file . ".php";
	}
	else
	{
		@include "rpd/" . $class_file . ".php";
		if (!class_exists($class_name) && !interface_exists($class_name))
		{
			require_once "app/" . $class_file . ".php";
		}
	}

}

/*
 * Make useable PHP environment
 */
include("rpd/_php/additional.php");			// generate additional functions

// workaround old versions
if (version_compare(PHP_VERSION, "5.3.0", "<"))
{
	include("rpd/_php/5-3-0.php");			// below 5.3.0
}

/**
 *
 * @bug server[redirect_url] contain "//" on calling root "/"
 */

ob_start();

Registry::parse_ini_file("configuration.php");	// some configuration

/**
 * SCHEME represent by which protocoll the client access the server
 */
define("SCHEME", ($_SERVER["SERVER_PORT"] == 443)? "https": "http");

/**
 * ROOT contains the directory where rpd runs
 */
define("ROOT", dirname($_SERVER["PHP_SELF"]));

/**
 * URL where rpd runs
 */
define("BASE", SCHEME . "://" . $_SERVER["HTTP_HOST"] . ROOT . "/");

require_once "bootstrap.php";			// user defined php before rpd acts

if (isset($_SERVER["REDIRECT_URL"]))
{
	$_SERVER["REDIRECT_URL"] = str_replace(ROOT,
	                                       "",
	                                       $_SERVER["REDIRECT_URL"]);
	                                                 
	$_SERVER["REDIRECT_URL"] = strtolower($_SERVER["REDIRECT_URL"]) . "/";
}
else
{
	$_SERVER["REDIRECT_URL"] = NULL;
}

$dir  = NULL;
$tmp  = strtok($_SERVER["REDIRECT_URL"], "/");	///< temporary variable
$file = Registry::get("site", "start");

/**
 *
 * Enter description here ...
 * @var string
 */
$ext = "." . pathinfo($_SERVER["REDIRECT_URL"], PATHINFO_EXTENSION);
define("EXTENSION", ($ext != ".")? $ext: NULL);

$view_path = Registry::get("view", "path");
$controller_path = Registry::get("controller", "path");

while ($tmp != false
       && (
           is_dir($view_path . "/" . $dir . $tmp . "/")
           || is_dir($contoller_path . "/" . $dir . $tmp . "/")
          )
      )
{
	/**
	 * @todo A1104 make bootstrap unreachable for clients
	 */

	// lookup bootstrap
	if (file_exists($controller_path . "/" . $dir . $tmp . "/bootstrap.php"))
	{
		$bootstrap[] = $controller_path . "/" . $dir . $tmp . "/bootstrap.php";
	}

	// go into dir
	$file = $tmp;
	$dir .= $tmp . "/";
	$tmp  = strtok("/");
}

if ($ext !== ".")
{
	$file .= EXTENSION;
	$tmp = basename($tmp, $ext);
	if (is_dir($view_path . "/" . $dir . $tmp . "/")
	|| is_dir($controller_path . "/" . $dir . $tmp . "/"))
	{
		$file = $tmp . EXTENSION;
		$dir .= $tmp . "/";
		$tmp  = strtok("/");
	}
}
else
{
	$ext = "";
}

/**
 *
 * Executed Directory.
 * \n
 * Depending on the called URL (by Client) the system searches the nearest
 * (lower) path wich contains a executable PHP-File. The name of this folder
 * is stored here.
 */
define("DIR", strtolower($dir));

/**
 *
 * Executed file.
 * \n
 * The client calls a URL to which the system searches the nearest (lower)
 * executable PHP-File. The name of this file is contained here.
 */

define("FILE", strtolower($file));

while ($tmp !== false)
{
	/**
	 * @todo C1101 url_decode needed here?
	 */
	$_GET[$tmp] = strtok("/");
	$tmp = strtok("/");
}

// run bootstraps for each path
if (isset($bootstrap)) foreach ($bootstrap as $file)
{
	include $file;
}

// create site if none is given
if (!isset($site))
{
	$site = new Controller();
}

echo $site;		// dispatch

?>